import {sys} from 'cc';

//TT 平台录屏
export default class TTRecorder
{

    public recorder = null;

    public clipPath: string = "";

    constructor()
    {
        this.recorder = tt.getGameRecorderManager();

        this.recorder.onStart(res =>
        {
            console.log("** tt record start");
            this.recorder.recordClip({
                timeRange: [1, 59]
            });
        });

        this.recorder.onPause(res =>
        {
            console.log('tt record pause');
        });

        this.recorder.onResume(res =>
        {
            console.log('tt record resume');
        });

        this.recorder.onStop(res =>
        {
            console.log('tt record stop');
            this.recorder.clipVideo({
                path: res.videoPath,
                timeRange: [50, 0],
                success: (r) =>
                {

                    this.clipPath = r.videoPath;
                    console.log("ClipV success=" + this.clipPath);
                },
                fail(e)
                {
                    console.log("** record error=" + e);
                }
            });
        });

        this.recorder.onError(res =>
        {
            console.log('tt record error');
        });

        this.recorder.onInterruptionBegin(res =>
        {
            console.log('tt record onInterruptionBegin');
        });

        this.recorder.onInterruptionEnd(res =>
        {
            console.log('tt record onInterruptionEnd');
        });
    }


    public shareClip(callback = null)
    {
        console.log("CLIPPATH=" + this.clipPath);
        if (this.clipPath !== "")
        {

            tt.shareVideo({
                videoPath: this.clipPath,
                success(res)
                {
                    console.log("视频分享成功");
                    if (callback !== null)
                        callback();
                },
                fail(res)
                {
                    console.log("视频分享失败");
                },
                extra: {createChallenge: true}
            });

        }
    }

    public shareClipAdv(title: string, desc: string, callback: Function = null)
    {
        if (this.clipPath !== "")
        {

            tt.shareAppMessage({
                channel: "video",

                extra: {
                    videoPath: this.clipPath,
                    videoTopics: ["脑力2248"],
                    hashtag_list: ["脑力2248"],
                    video_title: "2048遇上连连看!",
                    createChallenge: true
                },
                success()
                {
                    console.log("分享视频adv成功");
                    if (callback !== null)
                        callback();
                },
                fail(e)
                {
                    console.log("分享视频adv失败");
                }
            })
        }
    }

    public start(dur = 60)
    {
        this.recorder.start({duration: dur});
    }

    public stop()
    {
        this.recorder.stop();
    }
}
